#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

#include "debug.h"

bool MSDebug = true;
bool MS_touch_Debug = true;
char LOG_PATH[PATH_MAX] = "/mnt/sdcard/log_touch.txt";
const char* LOG_DIR = "/data/local/tmp/";

// #define log(LOG_PATH, ...) \
// {FILE *fp = fopen(LOG_PATH, "a+"); if (fp) {\
// 	fprintf(fp, __VA_ARGS__);\
//         fclose(fp);}}
#define log_to_file(...) \
{FILE *fp = fopen(LOG_PATH, "a+"); if (fp) {\
	fprintf(fp, __VA_ARGS__);\
        fclose(fp);}}

#define lprintf(format, ...) do { \
    fprintf(stderr, format,__VA_ARGS__); \
    fprintf(stderr, "\n"); \
} while (false)

void print_debug_info(const char* file, int line, const char* func, const char* fmt, ...)
{
	char temp[0x4000];

	if (!MSDebug)
	{
		return;
	}

	va_list args;

	memset(temp, 0, sizeof(temp));
	va_start(args, fmt);
	sprintf(temp, "V: %s|P: %d|T: %d [%s](%d):%s-->", MINICAP_VERSION, getpid(), gettid(), file, line, func);
	vsprintf(temp+strlen(temp), fmt, args);
	va_end(args);

	// printf("Maing:%d:%s\n", getpid(), temp);
	// log(log_path, "[%d] Maing(%s):%d:%s", getpid(), MINICAP_VERSION, gettid(), temp);
	// log_to_file("Maing: %s\n", temp);
	__android_log_print(ANDROID_LOG_DEBUG, LOG_MINICAP_TAG, "%s", temp);
}

void enable_debug_info(bool enable)
{
	MSDebug = enable;
}

// print_debug_info(__FILE__, __LINE__, "%s", __FUNCTION__);
void print_touch_debug_info(const char* file, int line, const char* func, const char* fmt, ...)
{
	char temp[0x4000];

	if (!MS_touch_Debug)
	{
		return;
	}

	va_list args;

	memset(temp, 0, sizeof(temp));
	va_start(args, fmt);
	sprintf(temp, "V: %s|P: %d|T: %d [%s](%d):%s-->", MINITOUCH_VERSION, getpid(), gettid(), file, line, func);
	vsprintf(temp+strlen(temp), fmt, args);
	va_end(args);

	// printf("Maing:%d:%s\n", getpid(), temp);
	// log(log_path, "[%d] Maing(%s):%d:%s", getpid(), MINICAP_VERSION, gettid(), temp);
	// log_to_file("Maing: %s\n", temp);
	__android_log_print(ANDROID_LOG_DEBUG, LOG_MINITOUCH_TAG, "%s", temp);

}

void enable_touch_debug_info(bool enable)
{
	MS_touch_Debug = enable;
}